<template>
  <div>
    <Card istitle>
      <template #title>添加账号</template>

      <!-- 账号添加from -->
      <template>
        <el-form
          :model="userForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          size="small"
        >
          <!-- 账号 -->
          <el-form-item label="账号" prop="name">
            <el-input
              type="text"
              v-model="userForm.name"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="userForm.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <!-- 用户组 -->
          <el-form-item label="用户组" prop="group">
            <el-select v-model="userForm.group" placeholder="请选择" clearable>
              <el-option label="普通管理员" value="普通管理员"> </el-option>
              <el-option label="超级管理员" value="超级管理员"> </el-option>
            </el-select>
          </el-form-item>

          <!-- 提交按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm('ruleForm')"
              :loading="submit"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
    </Card>
  </div>
</template>

<script>
import { validateName, validatePass } from '@/utils/validate.js'
import { addUser } from '@/api/user.js'
export default {
  data() {
    return {
      userForm: {
        name: '',
        password: '',
        group: ''
      },
      rules: {
        name: [{ validator: validateName, required: true, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }],
        group: [{ required: true, message: '请选择用户组', trigger: 'change' }]
      },
      submit: false
    }
  },
  methods: {
    submitForm(formName) {
      this.submit = true
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { code, msg } = await addUser({
            account: this.userForm.name,
            password: this.userForm.password,
            userGroup: this.userForm.group
          })
          if (code === 0) {
            this.$message({
              type: 'success',
              message: msg
            })
            setTimeout(() => {
              this.$router.push('/account/list')
            }, 1000)
          } else {
            this.submit = false
            this.$message.error(msg)
          }
        } else {
          this.submit = false
          console.log('error submit!!')
          setTimeout(() => {
            this.submit = false
          }, 1000)
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-input,
.el-select {
  width: 200px;
}
</style>
